<?php
class ControllerDelimanChucnang extends Controller
{
	private $error = array();
   	function __construct() 
	{
		$listchek = $this->getListCheck();
		if(in_array($this->getMethod(),$listchek))
		{
			if(!$this->user->hasPermission($this->getRoute(), $this->getMethod()))
			{
				$this->response->redirect("?route=common/permission");
			}
		}
		
		
	 	$this->load->model("deliman/chucnang");
		
		
   	}
	
	public function index()
	{
		$this->getList();
	}
	
	public function insert()
	{
		
		
    	$this->getForm();
	}
	
	public function update()
	{			
		$this->data['haspass'] = false;
		$this->data['readonly'] = 'readonly="readonly"';
		$this->data['class'] = 'readonly';
	
		$this->getForm();	
  	}
	
	public function delete() 
	{
		$listchucnangid=$this->request->post['delete'];
		if(count($listchucnangid))
		{
			foreach($listchucnangid as $chucnangid)
			{
				$this->model_deliman_chucnang->delete($chucnangid);	
			}
			$this->data['output'] = "true";
		}
		$this->id="content";
		$this->template="common/output.tpl";
		$this->render();
  	}
	
	private function getList() 
	{
		$this->data['insert'] = $this->url->http('deliman/chucnang/insert');
		$this->data['delete'] = $this->url->http('deliman/chucnang/delete');		
		
		$this->data['datas'] = array();
		$where = "";
		$rows = $this->model_deliman_chucnang->getList($where);
		//Page
		$page = $this->request->get['page'];		
		$x=$page;		
		$limit = 20;
		$total = count($rows); 
		// work out the pager values 
		$this->data['pager']  = $this->pager->pageLayout($total, $limit, $page); 
		
		$pager  = $this->pager->getPagerData($total, $limit, $page); 
		$offset = $pager->offset; 
		$limit  = $pager->limit; 
		$page   = $pager->page;
		for($i=$offset;$i < $offset + $limit && count($rows[$i])>0;$i++)
		//for($i=0; $i <= count($this->data['datas'])-1 ; $i++)
		{
			$this->data['datas'][$i] = $rows[$i];
			$this->data['datas'][$i]['link_edit'] = $this->url->http('deliman/chucnang/update&chucnangid='.$this->data['datas'][$i]['chucnangid']);
			$this->data['datas'][$i]['text_edit'] = "Edit";
			
			$this->data['datas'][$i]['link_hanhdong'] = $this->url->http('deliman/hanhdong&chucnangid='.$this->data['datas'][$i]['chucnangid']);
			$this->data['datas'][$i]['text_hanhdong'] = "Hành động";
		}
		$this->data['refres']=$_SERVER['QUERY_STRING'];
		$this->id='content';
		$this->template="deliman/chucnang_list.tpl";
		$this->layout="layout/deliman";
		
		$this->render();
	}
	
	
	private function getForm()
	{		
		
		if ((isset($this->request->get['chucnangid'])) ) 
		{
      		$this->data['item'] = $this->model_deliman_chucnang->getItem($this->request->get['chucnangid']);
    	}
		
		$this->id='content';
		$this->template='deliman/chucnang_form.tpl';
		$this->layout="layout/deliman";
		
		$this->render();
	}
	
	public function save()
	{
		$data = $this->request->post;
		if($this->validateForm($data))
		{
			if($data['chucnangid']=="")
			{
				$this->model_deliman_chucnang->insert($data);	
			}
			else
			{
				$this->model_deliman_chucnang->update($data);	
			}
			
			$this->data['output'] = "true";
		}
		else
		{
			foreach($this->error as $item)
			{
				$this->data['output'] .= $item."<br>";
			}
		}
		$this->id='content';
		$this->template='common/output.tpl';
		$this->render();
	}
	
	private function validateForm($data)
	{
		if (trim($data['machucnang']) == "") 
		{
      		$this->error['machucnang'] = "Bạn chưa nhập mã chức năng";
    	}
		else
		{
			$where = " AND machucnang = '".$data['machucnang']."'";
			$kybaos = $this->model_deliman_chucnang->getList($where);
			if(count($kybaos) > 0 && $data['chucnangid'] == "")
			{
				$this->error['machucnang'] = "Mã chức năng đã tồn tại trong hệ thống";	
			}
		}
		
		if (trim($data['tenchucnang']) == "") 
		{
      		$this->error['tenchucnang'] = "Bạn chưa nhập tên chức năng";
    	}
	
		if (count($this->error)==0) {
	  		return TRUE;
		} else {
	  		return FALSE;
		}
	}
	//Cac ham xu ly tren form
	
}
?>